package org.edu.modules.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.edu.modules.oa.entity.VeOaYinzhangLogEntity;
import org.edu.modules.oa.mapper.IVeOaYinzhangLogMapper;
import org.edu.modules.oa.service.IVeOaYinzhangLogService;
import org.edu.modules.oa.service.IVeOaYinzhangService;
import org.edu.modules.oa.vo.VeOaYinzhangLogVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class VeOaYinzhangLogServiceImpl extends ServiceImpl<IVeOaYinzhangLogMapper, VeOaYinzhangLogEntity> implements IVeOaYinzhangLogService {

    @Resource
    private IVeOaYinzhangLogMapper veOaYinzhangLogMapper;


    //查询（用户名，内容）
    @Override
    public HashMap<String, Object> showTable(VeOaYinzhangLogEntity veOaYinzhangLogEntity,List<String> nameList, Integer currentPage, Integer pageSize) {
        //创建分页的page
        IPage<VeOaYinzhangLogEntity> page = new Page<>(currentPage,pageSize);
        //创建查询的条件
        QueryWrapper<VeOaYinzhangLogEntity> queryWrapper = new QueryWrapper<>();

        if (nameList != null){
            for (int i=0;i<nameList.size();i++){
                queryWrapper.like("UserName",nameList.get(i));
                if (i == nameList.size() - 1){

                }else {
                    queryWrapper.or();
                }
            }
        }
        if (veOaYinzhangLogEntity.getDoSomething() != null && !veOaYinzhangLogEntity.getDoSomething().equals("")){
            queryWrapper.like("DoSomething",veOaYinzhangLogEntity.getDoSomething());
        }
        //印章日志类别
        if (veOaYinzhangLogEntity.getTypeStr() != null && !veOaYinzhangLogEntity.getTypeStr().equals("")){
            queryWrapper.like("typeStr",veOaYinzhangLogEntity.getTypeStr());
        }
        //倒序
        queryWrapper.orderByDesc("create_time");
        //条件+分页查询
        page = veOaYinzhangLogMapper.selectPage(page,queryWrapper);
        List<VeOaYinzhangLogEntity> entityList = page.getRecords();
        //查询表格总行数
        int count = veOaYinzhangLogMapper.selectCount(queryWrapper);
        //把查出来的东西存到map里面去
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",entityList);
        map.put("count",count);
        return map;
    }

    //逻辑删除（单/多）
    @Override
    public Integer deleteYinzhangLog(List<Long> ids) {
        Integer res = 0;
        res = veOaYinzhangLogMapper.deleteBatchIds(ids);
        return res;
    }

    //导出表格
    @Override
    public List<VeOaYinzhangLogVo> export(VeOaYinzhangLogEntity veOaYinzhangLogEntity) {
        //创建查询条件
        QueryWrapper<VeOaYinzhangLogEntity> queryWrapper = new QueryWrapper<>();
        //印章日志类别
        if (veOaYinzhangLogEntity.getTypeStr() != null && !veOaYinzhangLogEntity.getTypeStr().equals("")){
            queryWrapper.like("typeStr",veOaYinzhangLogEntity.getTypeStr());
        }
        //倒序
        queryWrapper.orderByDesc("create_time");
        //查询
        List<VeOaYinzhangLogEntity> entityList = veOaYinzhangLogMapper.selectList(queryWrapper);
        //集合voList是导出excel用的
        List<VeOaYinzhangLogVo> voList = new ArrayList<>();
        for (VeOaYinzhangLogEntity en:entityList){
            VeOaYinzhangLogVo vo = new VeOaYinzhangLogVo();
            //序号
            vo.setId(en.getId());
            //使用者账号
            vo.setUserId(en.getUserId());
            //用户名
            vo.setUserName(en.getUserName());
            //日志时间
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            vo.setCreateTime(formatter.format(en.getCreateTime()));
            //日志内容
            vo.setDoSomething(en.getDoSomething());
            //IP地址
            vo.setIpStr(en.getIpStr());
            //印章类型
            vo.setTypeStr(en.getTypeStr());

            voList.add(vo);
        }

        return voList;
    }
}
